package com.weipeep.demo;

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;

import java.io.IOException;

public class TextFooterEventHandler implements IEventHandler {
    private PdfFont pdfFont;
    private String classCode;
    private String printTime;
    public TextFooterEventHandler(PdfFont pdfFont, String classCode,String printTime,int totalPages) {
        this.pdfFont = pdfFont;
        this.classCode = classCode;
        this.printTime = printTime;
    }
    @Override
    public void handleEvent(Event event) {

        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfPage pdfPage = docEvent.getPage();
        PdfDocument pdfDoc = docEvent.getDocument();
        PdfCanvas pdfCanvas = new PdfCanvas(pdfPage);
        Rectangle pageSize = docEvent.getPage().getPageSize();
        Canvas canvas = new Canvas(pdfCanvas,pdfDoc,pageSize);
        UnitValue[] tableWidth = new UnitValue[]{new UnitValue(2,30),new UnitValue(2,30),new UnitValue(2,30)};
        Table table = new Table(tableWidth).setFont(pdfFont).setFontSize(8);
        table.addCell(new Cell().setBorder(Border.NO_BORDER).add(new Paragraph("打印日期："+printTime).setTextAlignment(TextAlignment.LEFT)));
        table.addCell(new Cell().setBorder(Border.NO_BORDER).add(new Paragraph("班号："+classCode).setTextAlignment(TextAlignment.CENTER)));
        table.addCell(new Cell().setBorder(Border.NO_BORDER));
        canvas.add(table.setFixedPosition(pageSize.getWidth()/2-200,10,new UnitValue(1,400)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        canvas.close();
        pdfCanvas.release();
    }

}